const Koa=require('koa');
const path=require('path')
const static=require('koa-static')
const body=require('koa-better-body')
const compress = require('koa-compress')
const session = require('koa-session')
let config=require('./libs/config.js')
let server=new Koa()
server.keys=['sadadewfwfw']
server.use(session({
  key: 'koa:sess', //cookie key (default is koa:sess)
  maxAge: 86400000, // cookie的过期时间 maxAge in ms (default is 1 days)
  overwrite: true, //是否可以overwrite    (默认default true)
  httpOnly: true, //cookie是否只有服务器端可以访问 httpOnly or not (default true)
  signed: true, //签名默认true
  rolling: false, //在每次请求时强行设置cookie，这将重置cookie过期时间（默认：false）
  renew: true //(boolean) renew session when session is nearly expired
},server))

server.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set("Access-Control-Allow-Credentials",true);
  await next();
 });


let router=require('./api/index.js')

const options = { threshold: 2048 };
server.use(compress(options));
server.context.db=require('./libs/database.js')
server.use(body({
  // uploadDir:path.resolve(__dirname,'./static/upload')
  uploadDir:path.resolve(__dirname,'./static/upload')
}))



server.use(static('./static'))
server.use(router.routes())
console.log(config.port)
server.listen(config.port)